﻿
// MotionControlSoftwareView.cpp : implementation of the CMotionControlSoftwareView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "MotionControlSoftware.h"
#endif

#include "MotionControlSoftwareDoc.h"
#include "MotionControlSoftwareView.h"
#include "MainFrm.h"
#include "dlg/DlgCircleDetail.h"

//#include "GDI/Delay.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

extern CCriticalSectionBaseAPI m_CriSection;
// CMotionControlSoftwareView

IMPLEMENT_DYNCREATE(CMotionControlSoftwareView, CScrollView)

BEGIN_MESSAGE_MAP(CMotionControlSoftwareView, CScrollView)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_SETCURSOR()
	ON_WM_LBUTTONDOWN()
	ON_WM_NCCALCSIZE()
	ON_WM_MOUSEWHEEL()
	ON_WM_HSCROLL()
	ON_WM_CLOSE()
	ON_WM_VSCROLL()
	ON_WM_TIMER()
	ON_WM_CHAR()
	ON_WM_KEYUP()
	ON_WM_KEYDOWN()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// CMotionControlSoftwareView construction/destruction

CMotionControlSoftwareView::CMotionControlSoftwareView()
{
	// TODO: add construction code here
// 	m_pDC = NULL;
// 	m_bFirstRun = TRUE;
// 	m_ClickCount = 0;
// 	m_bFirstMouseMove = FALSE;
// 	m_bActive= FALSE;
// 	m_iOnDraw = 0;
// 	m_InitWinCenterLogicPos.x = 0;
// 	m_InitWinCenterLogicPos.y = 0;
// 	m_iWinAndUserCoorRate = 0;
// 
// 	m_bScroll = FALSE;
// 	m_iWinAndUserCoorRate = 0;
// 	m_fInitWinAndUserCoorRateY = 0;
// 	m_iWinAndUserCoorRate = 0;
// 	m_fWinAndUserCoorRateY = 0;
// 	m_iViewType = 0;
	m_pDC = NULL;
	m_bFirstRun = TRUE;
	m_ClickCount = 0;
	m_bFirstMouseMove = FALSE;
	m_SmallGrid = 20;
	m_BigGrid = 50;
	m_RulerEdge = 100;
	m_gridSize = 10;
	m_iZoomRate = 0;
	m_iOldZoomRate = 0;
	m_CenterMove.cx = 0;
	m_CenterMove.cy = 0;
	m_LeftTopMove.cx = 0;
	m_LeftTopMove.cy = 0;
	m_dRulerUnit = 1.0;
	m_dNOMultipleUnit = m_dRulerUnit;			
	m_dNOMultiple = 1;
	m_bActive= FALSE;
	m_iOnDraw = 0;
	m_InitWinCenterLogicPos.x = 0;
	m_InitWinCenterLogicPos.y = 0;
	m_iWinAndUserCoorRate = 0;
	m_bMoveSelectCenter = false;
	m_bUpOutLine = false;
	m_bFirstMoveSelectCenter = true;
}

CMotionControlSoftwareView::~CMotionControlSoftwareView()
{
}

BOOL CMotionControlSoftwareView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

// CMotionControlSoftwareView drawing
#include <afxcontrolbarutil.h>
void CMotionControlSoftwareView::OnDraw(CDC* pDC)
{
	// TODO: add draw code for native data here
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	CMainFrame* pMainFrame = (CMainFrame*)(AfxGetApp()->m_pMainWnd);
	CMotionControl* pMC = &pMainFrame->m_wndMotionView.m_DlgMC.m_MC;
	// 	if (pDoc->m_param.m_viewPara.m_dAxisX != pMC->m_ParamSet->m_viewPara.m_dAxisX ||
	// 		pDoc->m_param.m_viewPara.m_dAxisY != pMC->m_ParamSet->m_viewPara.m_dAxisY)
	// 	{
	// 		m_iOnDraw = 1;
	// 		pDoc->ParamToDocAll();
	// 	}
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	m_iOnDraw ++;

	CRect rect;
	GetClientRect(rect);

	CDC dcDraw;
	dcDraw.CreateCompatibleDC(pDC);
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	dcDraw.SelectObject(&bitmap);

	CBrush brush;
	brush.CreateStockObject(WHITE_BRUSH); //创建白色画刷。
	dcDraw.FillRect(rect,&brush); //用该白色画刷填充矩形。

	CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); 	 
	CBrush * oldbrush = dcDraw.SelectObject(pBrush); //创建透明画刷

	//	OnPrepareDC(&dcDraw);//调整图形设备显示 

	SetBasicalCoordinateSys(pDoc,&dcDraw);
	m_CriSection.Lock();
	DrawAllGraph(pDoc,&dcDraw);
	m_CriSection.Unlock();

	pDC->SetViewportOrg(0,0);
	pDC->SetWindowOrg(0,0);
	pDC->SetMapMode(MM_TEXT);

	dcDraw.SetViewportOrg(0,0);
	dcDraw.SetWindowOrg(0,0);
	dcDraw.SetMapMode(MM_TEXT);

	pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dcDraw, 0, 0, SRCCOPY);

	dcDraw.SelectObject(oldbrush);
	dcDraw.DeleteDC(); //删除DC 
	bitmap.DeleteObject(); //删除位图

	//	::PostMessage(this->g,SELECTGRAPH,0,0);
	
}

void CMotionControlSoftwareView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	SetScrollSizes(MM_LOMETRIC, CSize(64000,64000));
	ModifyStyle(WS_HSCROLL|WS_VSCROLL,0);
	SetTimer(6789,200,NULL);
}
void CMotionControlSoftwareView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
//	OnContextMenu(this, point);
}

void CMotionControlSoftwareView::OnContextMenu(CWnd* pWnd, CPoint point)
{
//	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_NOELEMENT_MENU,point.x,point.y,this);
}


// CMotionControlSoftwareView diagnostics

#ifdef _DEBUG
void CMotionControlSoftwareView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CMotionControlSoftwareView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CMotionControlSoftwareDoc* CMotionControlSoftwareView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMotionControlSoftwareDoc)));
	return (CMotionControlSoftwareDoc*)m_pDocument;
}
#endif //_DEBUG


// CMotionControlSoftwareView message handlers
BOOL CMotionControlSoftwareView::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	CMotionControlSoftwareDoc* pDoc = GetDocument();

	if (pMsg->message == WM_KEYDOWN )	//选中图形的删除
	{
		if(pMsg->wParam == VK_DELETE)
		{
			int iSelectNum = pDoc->m_graphSelectedList.GetCount();
			if(iSelectNum>0)
			{
				for (int i=0; i<iSelectNum;i++)
				{
					UINT pPtr = pDoc->m_graphSelectedList[i];
					if (pPtr != NULL)
					{
						CBlock* pCurBlock = (CBlock*) pPtr;
						pDoc->m_graphListTop.Remove(pCurBlock);
					}
				}
				pDoc->m_graphSelectedList.RemoveAll();
			
				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndPropertiesView.m_propertyView.GetSafeHwnd(),SELECTGRAPH,0,1);
			
				pDoc->UpdateView();
			}
		}
	}

/*	if (pMsg->message == WM_VSCROLL)
	{
		return FALSE;
	}
	else if (pMsg->message == WM_HSCROLL)
	{
		return FALSE;
	}
	else if (pMsg->message == WM_MOUSEWHEEL)
	{
		return FALSE;
	}
*/
	return CScrollView::PreTranslateMessage(pMsg);
}


int CMotionControlSoftwareView::GetNearestZoomRate(double dBaseRate)
{
	int iZoomRate=0;
	if (dBaseRate < 0)
	{
		AfxMessageBox(_T("比例为负数出错"));
		return -1;
	}
	else if (dBaseRate < 1)
	{
		int iZoomCur = -1;
		while (TRUE)
		{
			double dCurValue = pow(1.2,iZoomCur);
			if (dCurValue - dBaseRate < 0)
			{
				break;
			}
			iZoomCur --;
		}

		double dSmallValueLog = pow(1.2,iZoomCur);
		double dBigValueLog = pow(1.2,iZoomCur+1);
		double dSmallValue = dBaseRate - dSmallValueLog;
		double dBigValue = dBigValueLog - dBaseRate;
		if (dSmallValue < dBigValue)
		{
			iZoomRate = iZoomCur;
		}
		else
		{
			iZoomRate = iZoomCur+1;
		}
	}
	else if (dBaseRate > 1)
	{
		int iZoomCur = 1;
		while (TRUE)
		{
			double dCurValue = pow(1.2,iZoomCur);
			if (dCurValue - dBaseRate > 0)
			{
				break;
			}
			iZoomCur ++;
		}

		double dSmallValueLog = pow(1.2,iZoomCur-1);
		double dBigValueLog = pow(1.2,iZoomCur);
		double dSmallValue = dBaseRate - dSmallValueLog;
		double dBigValue = dBigValueLog - dBaseRate;
		if (dSmallValue < dBigValue)
		{
			iZoomRate = iZoomCur - 1;
		}
		else
		{
			iZoomRate = iZoomCur;
		}
	}
	return iZoomRate;
}

void CMotionControlSoftwareView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CClientDC dc(this);
	OnPrepareDC(&dc);
	//显示预览图片
	m_pDC = &dc;
	if (m_ClientRect.IsRectNull())
	{
		GetClientRect(&m_ClientRect);
	}

	CPoint pt;
	CRect rect;
	pt.x = point.x;
	pt.y = point.y;
	dc.DPtoLP(&pt);
	rect.left = pt.x +20;
	rect.right = pt.x + 70;
	rect.top = pt.y-20;
	rect.bottom = pt.y -70;

	if (pDoc->m_DrawType != -1)
	{	
		if((m_ClickCount == 1))
		{
			SetBasicalCoordinateSys(pDoc,&dc);
			DrawAllGraph(pDoc,&dc);
		}
	}
	if (pDoc->m_DrawType == 0)
	{
		if (m_ClickCount == 1)
		{
			if(m_bFirstMoveSelectCenter)
			{
				m_currentPoint = m_firstPoint;
				m_bFirstMoveSelectCenter = false;
			}
			if(m_bMoveSelectCenter)	//选中平移
			{
				CPoint pointMove;
				CPointEx exMove1,exMove2;
				exMove1 = LPointToMyPoint(m_currentPoint);
				exMove2 = LPointToMyPoint(pt);
				exMove2.m_dPX = exMove2.m_dPX - exMove1.m_dPX;
				exMove2.m_dPY = exMove2.m_dPY - exMove1.m_dPY;
				pDoc->OnSelectMove(exMove2);
			}
			else	//框选
			{
				m_bBeginSelect = true;
				CPoint movePt, centerPt;			
				CRect rectOld, rectNew;
				if (m_currentPoint.x - m_firstPoint.x < 0)
				{
					rectOld.left = m_currentPoint.x;
					rectOld.right = m_firstPoint.x;
				}
				else
				{
					rectOld.left = m_firstPoint.x;
					rectOld.right = m_currentPoint.x;
				}
				if (m_currentPoint.y - m_firstPoint.y < 0)
				{
					rectOld.top = m_currentPoint.y;
					rectOld.bottom = m_firstPoint.y;
				}
				else
				{
					rectOld.top = m_firstPoint.y;
					rectOld.bottom = m_currentPoint.y;
				}
				CBrush* pOldBrush = NULL;
				CBrush pNewBrush;
				pNewBrush.CreateStockObject(NULL_BRUSH);
				pOldBrush = dc.SelectObject(&pNewBrush);

				CPen penInverse(PS_DOT,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
				CPen* pOldPen;		
				pOldPen = dc.SelectObject(&penInverse);
				dc.SelectObject(::GetStockObject(NULL_BRUSH));
				dc.Rectangle(&rectOld);

				if (m_firstPoint.x - pt.x < 0)
				{
					rectNew.left = m_firstPoint.x;
					rectNew.right = pt.x;
					centerPt.x = m_firstPoint.x + (pt.x - m_firstPoint.x) / 2;
				}
				else
				{
					rectNew.left = pt.x;
					rectNew.right = m_firstPoint.x;
					centerPt.x = pt.x + (m_firstPoint.x - pt.x) / 2;
				}
				if (m_firstPoint.y - pt.y < 0)
				{
					rectNew.top = m_firstPoint.y;
					rectNew.bottom = pt.y;
					centerPt.y = m_firstPoint.y + (pt.y - m_firstPoint.y) / 2;
				}
				else
				{
					rectNew.top = pt.y;
					rectNew.bottom = m_firstPoint.y;
					centerPt.y = pt.y + (m_firstPoint.y - pt.y) / 2;
				}
				CPen penDraw(PS_DOT,0,RGB(0,0,255));
				dc.SelectObject(&penDraw);
				dc.Rectangle(&rectNew);		
				dc.SelectObject(pOldPen);
				dc.SelectObject(pOldBrush);
			}
			m_currentPoint = pt;
		}
		else if(m_ClickCount == 0)	//选中平移显示
		{
			if ((pDoc->m_graphSelectedList.GetCount() > 0) //选择大于一个
				&& (!(GetKeyState(VK_CONTROL)< 0 ? TRUE : FALSE))) //没有按住Ctrl
			{
				CRectEx curRect;
				if(pDoc->GetSelectedRect(curRect) == 0)
				{
					double dCLX = (curRect.m_left+curRect.m_right)/2.0;
					double dCLY = (curRect.m_top+curRect.m_bottom)/2.0;
					
					int dX = dCLX*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.x;
					int dY = dCLY*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.y;

					double dPPos = sqrt((double)((dX-pt.x)*(dX-pt.x)+(dY-pt.y)*(dY-pt.y)));
					if(dPPos<15)
					{
						m_bMoveSelectCenter = true;
					}
					else
					{
						m_bMoveSelectCenter = false;
					}
				}
			}
			else
			{
				m_bUpOutLine = false;
				CPointEx ptClick;
				ptClick.m_dPX = (double)(pt.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
				ptClick.m_dPY = (double)(pt.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
				for (std::vector<CBlock*>::iterator it=pDoc->m_graphListTop.begin(); it!= pDoc->m_graphListTop.end(); it++)
				{
					CBlock* pCurBlock = (CBlock*)(*it);
					//if (pCurBlock->m_cRRect.IsPtInRect(ptClick))
					{
						if (pCurBlock->PtUpOutLine(ptClick,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate))
						{
							m_bUpOutLine = true;
							break;
						}
					}
				}
				if (m_bUpOutLine)
				{
					::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));
					//::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CURSOR2));
				}
			}
			m_bFirstMoveSelectCenter = true;
		}
	}
	else if (pDoc->m_DrawType == 1)//画线
	{
		if (m_ClickCount == 1)
		{
			CPen penInverse(PS_DOT,0,RGB(255,255,255));
			CPen* pOldPen;
			pOldPen = m_pDC->SelectObject(&penInverse);
			dc.MoveTo(m_firstPoint);
			dc.LineTo(m_currentPoint);

			CPen penDraw(PS_DOT,0,RGB(0,0,255));
			pOldPen = m_pDC->SelectObject(&penDraw);
			dc.MoveTo(m_firstPoint);
			dc.LineTo(pt);
			dc.SelectObject(pOldPen);
			m_currentPoint = pt;
		}
		CPen penInverse(PS_SOLID,0,RGB(255,255,255));
		CPen* pOldPen;
		pOldPen = m_pDC->SelectObject(&penInverse);
		dc.MoveTo(m_oldRectCursor.right,m_oldRectCursor.top);
		dc.LineTo(m_oldRectCursor.left,m_oldRectCursor.bottom);
		CPen penDraw(PS_SOLID,0,RGB(0,0,255));
		pOldPen = m_pDC->SelectObject(&penDraw);
		dc.MoveTo(rect.right,rect.top);
		dc.LineTo(rect.left,rect.bottom);
		dc.SelectObject(pOldPen);
		m_oldRectCursor = rect;
	}
	else if (pDoc->m_DrawType == 2)//画圆弧
	{
		if (m_startMadeArc)
		{
			if (m_ClickCount == 1)
			{
				CPoint centerPt = m_firstPoint;
				double iradius = m_viewMath.BetweenPointLen(centerPt,m_currentPoint);
				CPoint movePt;

				CPen penInverse(PS_DOT,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
				CPen* pOldPen;		
				pOldPen = dc.SelectObject(&penInverse);
				//默认逆时针
				movePt.x = centerPt.x + cos((0)*M_PI/180)* iradius;
				movePt.y = centerPt.y + sin((0)*M_PI/180)* iradius;
				dc.MoveTo(movePt);
				dc.AngleArc(centerPt.x,centerPt.y,iradius, 0, 360);
				CPen penDraw(PS_DOT,0,RGB(0,0,255));
				pOldPen = dc.SelectObject(&penDraw);
				iradius = m_viewMath.BetweenPointLen(centerPt,pt);
				movePt.x = centerPt.x + cos((0)*M_PI/180)* iradius;
				movePt.y = centerPt.y + sin((0)*M_PI/180)* iradius;
				dc.MoveTo(movePt);
				dc.AngleArc(centerPt.x,centerPt.y,iradius, 0, 360);

				dc.SelectObject(pOldPen);
				m_currentPoint = pt;
			}
		}

		CPoint centerPt = m_oldRectCursor.CenterPoint();
		double iradius = m_oldRectCursor.Width()/2;
		CPoint movePt;

		CPen penInverse(PS_SOLID,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
		CPen* pOldPen;		
		pOldPen = dc.SelectObject(&penInverse);
		//默认逆时针
		movePt.x = centerPt.x + cos((0)*M_PI/180)* iradius;
		movePt.y = centerPt.y + sin((0)*M_PI/180)* iradius;
		dc.MoveTo(movePt);
		dc.AngleArc(centerPt.x,centerPt.y,iradius, 0, 360);
		CPen penDraw(PS_SOLID,0,RGB(0,0,255));
		pOldPen = dc.SelectObject(&penDraw);
		centerPt = rect.CenterPoint();
		movePt.x = centerPt.x + cos((0)*M_PI/180)* iradius;
		movePt.y = centerPt.y + sin((0)*M_PI/180)* iradius;
		dc.MoveTo(movePt);
		dc.AngleArc(centerPt.x,centerPt.y,iradius, 0, 360);
		dc.SelectObject(pOldPen);
		m_oldRectCursor = rect;
	}
	else if (pDoc->m_DrawType == 3)//画椭圆弧
	{
		if (m_ClickCount == 1)
		{
			if (!m_bFirstMouseMove)
			{
				CPoint movePt, centerPt;			
				CRect rectOld, rectNew;
				if (m_currentPoint.x - m_firstPoint.x < 0)
				{
					rectOld.left = m_currentPoint.x;
					rectOld.right = m_firstPoint.x;
					centerPt.x = m_currentPoint.x + (m_firstPoint.x - m_currentPoint.x) / 2;
				}
				else
				{
					rectOld.left = m_firstPoint.x;
					rectOld.right = m_currentPoint.x;
					centerPt.x = m_firstPoint.x + (m_currentPoint.x - m_firstPoint.x) / 2;
				}
				if (m_currentPoint.y - m_firstPoint.y < 0)
				{
					rectOld.top = m_currentPoint.y;
					rectOld.bottom = m_firstPoint.y;
					centerPt.y = m_currentPoint.y +(m_firstPoint.y - m_currentPoint.y) / 2;
				}
				else
				{
					rectOld.top = m_firstPoint.y;
					rectOld.bottom = m_currentPoint.y;
					centerPt.y = m_firstPoint.y + (m_currentPoint.y - m_firstPoint.y) / 2;
				}
				CBrush* pOldBrush = NULL;
				CBrush pNewBrush;
				pNewBrush.CreateStockObject(NULL_BRUSH);
				pOldBrush = dc.SelectObject(&pNewBrush);

				CPen penInverse(PS_DOT,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
				CPen* pOldPen;		
				pOldPen = dc.SelectObject(&penInverse);
				dc.SelectObject(::GetStockObject(NULL_BRUSH));
				dc.Ellipse(&rectOld);

				if (m_firstPoint.x - pt.x < 0)
				{
					rectNew.left = m_firstPoint.x;
					rectNew.right = pt.x;
					centerPt.x = m_firstPoint.x + (pt.x - m_firstPoint.x) / 2;
				}
				else
				{
					rectNew.left = pt.x;
					rectNew.right = m_firstPoint.x;
					centerPt.x = pt.x + (m_firstPoint.x - pt.x) / 2;
				}
				if (m_firstPoint.y - pt.y < 0)
				{
					rectNew.top = m_firstPoint.y;
					rectNew.bottom = pt.y;
					centerPt.y = m_firstPoint.y + (pt.y - m_firstPoint.y) / 2;
				}
				else
				{
					rectNew.top = pt.y;
					rectNew.bottom = m_firstPoint.y;
					centerPt.y = pt.y + (m_firstPoint.y - pt.y) / 2;
				}
				CPen penDraw(PS_DOT,0,RGB(0,0,255));
				dc.SelectObject(&penDraw);
				dc.Ellipse(&rectNew);		
				dc.SelectObject(pOldPen);
				dc.SelectObject(pOldBrush);
			}
			else
			{
				m_bFirstMouseMove = FALSE;
			}
			m_currentPoint = pt;
		}
		CBrush* pOldBrush = NULL;
		CBrush pNewBrush;
		pNewBrush.CreateStockObject(NULL_BRUSH);
		pOldBrush = dc.SelectObject(&pNewBrush);
		CPen penInverse(PS_SOLID,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
		CPen* pOldPen;		
		pOldPen = dc.SelectObject(&penInverse);
		dc.SelectObject(::GetStockObject(NULL_BRUSH));
		dc.Ellipse(&m_oldRectCursor);
		rect.top = rect.top - (rect.Height())/4 + 5;
		rect.bottom = rect.bottom + (rect.Height())/4 + 5;
		rect.left = rect.left - (rect.Width())/2 + 5;
		rect.right = rect.right + (rect.Width())/2 + 5;
		CPen penDraw(PS_SOLID,0,RGB(0,0,255));
		dc.SelectObject(&penDraw);
		dc.Ellipse(&rect);		
		dc.SelectObject(pOldPen);
		dc.SelectObject(pOldBrush);
		m_oldRectCursor =rect;
	}
	else if (pDoc->m_DrawType == 4)//画矩形
	{
		if (m_ClickCount == 1)
		{
			if (!m_bFirstMouseMove)
			{
				CPoint movePt, centerPt;			
				CRect rectOld, rectNew;
				if (m_currentPoint.x - m_firstPoint.x < 0)
				{
					rectOld.left = m_currentPoint.x;
					rectOld.right = m_firstPoint.x;
				}
				else
				{
					rectOld.left = m_firstPoint.x;
					rectOld.right = m_currentPoint.x;
				}
				if (m_currentPoint.y - m_firstPoint.y < 0)
				{
					rectOld.top = m_currentPoint.y;
					rectOld.bottom = m_firstPoint.y;
				}
				else
				{
					rectOld.top = m_firstPoint.y;
					rectOld.bottom = m_currentPoint.y;
				}
				CBrush* pOldBrush = NULL;
				CBrush pNewBrush;
				pNewBrush.CreateStockObject(NULL_BRUSH);
				pOldBrush = dc.SelectObject(&pNewBrush);

				CPen penInverse(PS_DOT,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
				CPen* pOldPen;		
				pOldPen = dc.SelectObject(&penInverse);
				dc.SelectObject(::GetStockObject(NULL_BRUSH));
				dc.Rectangle(&rectOld);

				if (m_firstPoint.x - pt.x < 0)
				{
					rectNew.left = m_firstPoint.x;
					rectNew.right = pt.x;
					centerPt.x = m_firstPoint.x + (pt.x - m_firstPoint.x) / 2;
				}
				else
				{
					rectNew.left = pt.x;
					rectNew.right = m_firstPoint.x;
					centerPt.x = pt.x + (m_firstPoint.x - pt.x) / 2;
				}
				if (m_firstPoint.y - pt.y < 0)
				{
					rectNew.top = m_firstPoint.y;
					rectNew.bottom = pt.y;
					centerPt.y = m_firstPoint.y + (pt.y - m_firstPoint.y) / 2;
				}
				else
				{
					rectNew.top = pt.y;
					rectNew.bottom = m_firstPoint.y;
					centerPt.y = pt.y + (m_firstPoint.y - pt.y) / 2;
				}
				CPen penDraw(PS_DOT,0,RGB(0,0,255));
				dc.SelectObject(&penDraw);
				dc.Rectangle(&rectNew);		
				dc.SelectObject(pOldPen);
				dc.SelectObject(pOldBrush);
			}
			else
			{
				m_bFirstMouseMove = FALSE;
			}
			m_currentPoint = pt;
		}
		CBrush* pOldBrush = NULL;
		CBrush pNewBrush;
		pNewBrush.CreateStockObject(NULL_BRUSH);
		pOldBrush = dc.SelectObject(&pNewBrush);
		CPen penInverse(PS_SOLID,0,RGB(255,255,255));//::GetSysColor(COLOR_BTNFACE)
		CPen* pOldPen;		
		pOldPen = dc.SelectObject(&penInverse);
		dc.SelectObject(::GetStockObject(NULL_BRUSH));
		dc.Rectangle(&m_oldRectCursor);
		CPen penDraw(PS_SOLID,0,RGB(0,0,255));
		dc.SelectObject(&penDraw);
		dc.Rectangle(&rect);		
		dc.SelectObject(pOldPen);
		dc.SelectObject(pOldBrush);
		m_oldRectCursor =rect;
	}
	else if (pDoc->m_DrawType == 6)
	{
		if (m_ClickCount == 1)
		{
			ScreenToClient(&point);
			m_secondPoint = point;
			dc.DPtoLP(&m_secondPoint);
			m_CenterMove.cx = m_firstPoint.x - m_secondPoint.x;
			m_CenterMove.cy = m_firstPoint.y - m_secondPoint.y;
			Invalidate(TRUE);
		}
	}

// 	SetBasicalCoordinateSys(pDoc,&dc);
// 	DrawAllGraph(pDoc,&dc);
	CScrollView::OnMouseMove(nFlags, point);
}


void CMotionControlSoftwareView::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
// 	if (this == GetCapture())
// 	{
// 		ReleaseCapture();
// 	}
// 	CMotionControlSoftwareDlg * pWnd = (CMotionControlSoftwareDlg*)GetParent();
// 	pWnd->PostMessage(ID_MES_INITOOLBARGRAPH,0,0);

	CClientDC dc(this);
	OnPrepareDC(&dc);
	m_pDC = &dc;

// 	//清除选择链表
// 	pDoc->ClearSelectList();
// 
// 	pDoc->m_DrawType = 0;
// 	m_ClickCount = 0;
// 	Invalidate(TRUE);
// 	::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);

	if (pDoc->m_graphSelectedList.GetCount()>0)
	{
		CMenu menu,*pSubMenu;//定义下面要用到的cmenu对象  
		menu.LoadMenu(IDR_MENU_RIGHTOFTREE);//装载自定义的右键菜单  
		pSubMenu=menu.GetSubMenu(0);//获取第一个弹出菜单，所以第一个菜单必须有子菜单  
		//pSubMenu->EnableMenuItem(ID_MU_SHOWEZDFILE,MF_DISABLED|MF_GRAYED);
		if(pSubMenu != NULL)
		{
			CPoint oPoint;//定义一个用于确定光标位置的位置  
			GetCursorPos(&oPoint);//获取当前光标的位置，以便使得菜单可以跟随光标  
			pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,oPoint.x,oPoint.y,this);   //在指定位置显示弹出菜单	
		}
	}
	else
	{
		pDoc->m_DrawType = 0;
		//清除选择链表
		pDoc->ClearSelectList();
		Invalidate(TRUE);
		::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
	}


	CScrollView::OnRButtonDown(nFlags, point);
}


void CMotionControlSoftwareView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	OnPrepareDC(&dc);

	//显示预览图片
	m_pDC = &dc;
	CPointEx begPt, endPt;
	if (m_ClientRect.IsRectNull())
	{
		GetClientRect(&m_ClientRect);
	}
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	if (pDoc->m_DrawType == 6)
	{
		m_ClickCount = 0;
	}
	else if (pDoc->m_DrawType == 0)
	{
		if (m_ClickCount == 1)
		{
			if (m_bBeginSelect)
			{
				bool bNegative = false;
				m_dStartDegree = 0;
				m_dSweepDegree = 360;	
				m_ClickCount ++;
				m_secondPoint = point;
				m_pDC->DPtoLP(&m_secondPoint);

				begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
				begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
				endPt.m_dPX = (double)(m_secondPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
				endPt.m_dPY = (double)(m_secondPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;			
				m_dRadiusX = fabs(begPt.m_dPX - endPt.m_dPX);
				m_dRadiusY = fabs(begPt.m_dPY - endPt.m_dPY);
				CPointEx centerPt, tempPtLeftTop, tempPtRightBottom;
				if ((begPt.m_dPX - endPt.m_dPX) >= 0)
				{
					centerPt.m_dPX = endPt.m_dPX + (begPt.m_dPX - endPt.m_dPX)/2;
					tempPtLeftTop.m_dPX = endPt.m_dPX;
					tempPtRightBottom.m_dPX = begPt.m_dPX;

					bNegative = true;
				}
				else
				{
					centerPt.m_dPX = begPt.m_dPX + (endPt.m_dPX - begPt.m_dPX)/2;
					tempPtLeftTop.m_dPX = begPt.m_dPX;
					tempPtRightBottom.m_dPX = endPt.m_dPX;
				}
				if ((begPt.m_dPY - endPt.m_dPY) >= 0)
				{
					centerPt.m_dPY = endPt.m_dPY + (begPt.m_dPY - endPt.m_dPY)/2;
					tempPtRightBottom.m_dPY = endPt.m_dPY;
					tempPtLeftTop.m_dPY = begPt.m_dPY;
				}
				else
				{
					centerPt.m_dPY = begPt.m_dPY + (endPt.m_dPY - begPt.m_dPY)/2;
					tempPtRightBottom.m_dPY = begPt.m_dPY;
					tempPtLeftTop.m_dPY = endPt.m_dPY;
				}

				begPt = tempPtLeftTop;
				endPt = tempPtRightBottom;

				CRectangle* theRectangle = new CRectangle;
				theRectangle->Create(begPt, endPt, centerPt);

				GetSelectedList(pDoc,theRectangle->m_cRRect,bNegative);
				Invalidate(TRUE);
				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
//				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndPropertiesView.m_propertyView.GetSafeHwnd(),SELECTGRAPH,0,1);
			}
			m_ClickCount = 0;
			m_bMoveSelectCenter = false;
			m_bFirstMoveSelectCenter = true;
		//	::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndPropertiesView.m_propertyView.GetSafeHwnd(),SELECTGRAPH,0,1);
		}
	}
	
	CScrollView::OnLButtonUp(nFlags, point);
}

BOOL CMotionControlSoftwareView::GetSelectedListForClick(CMotionControlSoftwareDoc* pDoc,CPointEx ptClick)
{
	for (std::vector<CBlock*>::iterator it=pDoc->m_graphListTop.begin(); it!= pDoc->m_graphListTop.end(); it++)
	{
		CBlock* pCurBlock = (CBlock*)(*it);
//		if (pCurBlock->m_cRRect.IsPtInRect(ptClick))
		{
			if (pCurBlock->PtUpOutLine(ptClick,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate))
			{
				pDoc->m_graphSelectedList.Add((UINT)pCurBlock);
				pCurBlock->m_bSelected = TRUE;
				break;
			}
		}
	}
	if (pDoc->m_graphSelectedList.GetCount() > 0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
BOOL CMotionControlSoftwareView::GetSelectedList(CMotionControlSoftwareDoc* pDoc,CRectEx rect,bool bNegative)
{
	for (std::vector<CBlock*>::iterator it=pDoc->m_graphListTop.begin(); it!= pDoc->m_graphListTop.end(); it++)
	{
		CBlock* pCurBlock = (CBlock*)(*it);
		pCurBlock->UpdateUnionRectExRecursion();	//针对文字出现CElement对象主动更新rect，未通知graph的情况
		if(bNegative)
		{
			if (pCurBlock->m_cRRect.IsIntersection(pCurBlock->m_cRRect,rect) || pCurBlock->m_cRRect.IsSubset(rect))
			{
				if(pCurBlock->m_bSelected)
				{
					pCurBlock->m_bSelected = false;

					int iSelNum = 0;
					for (int i=0; i<pDoc->m_graphSelectedList.GetCount();i++)
					{
						UINT pPtr = pDoc->m_graphSelectedList[i];
						if (pPtr = ((UINT)pCurBlock))
						{
							iSelNum = i;
							break;
						}
					}
					pDoc->m_graphSelectedList.RemoveAt(iSelNum);
				}
				else
				{
					pCurBlock->m_bSelected = TRUE;
					pDoc->m_graphSelectedList.Add((UINT)pCurBlock);
				}
			}
		}
		else
		{
			if (pCurBlock->m_cRRect.IsSubset(rect))
			{
				if(pCurBlock->m_bSelected)
				{
					pCurBlock->m_bSelected = false;

					int iSelNum = 0;
					for (int i=0; i<pDoc->m_graphSelectedList.GetCount();i++)
					{
						UINT pPtr = pDoc->m_graphSelectedList[i];
						if (pPtr = ((UINT)pCurBlock))
						{
							iSelNum = i;
							break;
						}
					}
					pDoc->m_graphSelectedList.RemoveAt(iSelNum);
				}
				else
				{
					pDoc->m_graphSelectedList.Add((UINT)pCurBlock);
					pCurBlock->m_bSelected = TRUE;
				}
			}
		}
	}
	if (pDoc->m_graphSelectedList.GetCount() > 0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CMotionControlSoftwareView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: Add your message handler code here and/or call default
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	if (nHitTest == HTCLIENT)
	{
		if (pDoc->m_DrawType != 0)
		{
			if (pDoc->m_DrawType == 1)
			{
				//::SetCursor(AfxGetApp()->LoadCursor(IDC_CROSS));
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			else if (pDoc->m_DrawType == 2)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			else if (pDoc->m_DrawType == 3)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			else if (pDoc->m_DrawType == 4)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			else if (pDoc->m_DrawType == 5)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
			}
			else if (pDoc->m_DrawType == 6)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEALL));
			}
		}
		else
		{
			if(m_bMoveSelectCenter)
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_SIZEALL));
			}
			else
			{
				::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
			}
		}		
		return TRUE;
	}
	return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}


void CMotionControlSoftwareView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	CPointEx begPt, endPt;
	if (pDoc->m_DrawType == 0)
	{
		m_ClickCount = 0;
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);
		if (m_bUpOutLine)
		{
			BOOL bCtrl = GetKeyState(VK_CONTROL)< 0 ? TRUE : FALSE;
			if (!bCtrl)
			{
				//没有按住Ctrl才是单选
				if (!pDoc->m_graphSelectedList.IsEmpty())
				{
					pDoc->ClearSelectList();
				}
			}
			//点选
			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
			if (GetSelectedListForClick(pDoc,begPt))
			{
				Invalidate(TRUE);
				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
				::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndPropertiesView.m_propertyView.GetSafeHwnd(),SELECTGRAPH,0,1);
			}
			m_bUpOutLine = false;
		}
		else
		{
			m_ClickCount ++;
			m_bBeginSelect = false;
			if (!m_bMoveSelectCenter)
			{
				BOOL bCtrl = GetKeyState(VK_CONTROL)< 0 ? TRUE : FALSE;
				if (!bCtrl)
				{
					if (!pDoc->m_graphSelectedList.IsEmpty())
					{
						pDoc->ClearSelectList();
						Invalidate(TRUE);
						::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
					}
				}
			}
		}
	}
	else if (pDoc->m_DrawType == 1)//画线
	{
		if (m_ClickCount == 0)
		{
			m_firstPoint = point;
			dc.DPtoLP(&m_firstPoint);
			m_ClickCount ++;
		}
		else if (m_ClickCount == 1)
		{ 
			m_ClickCount ++;
			m_secondPoint = point;
			dc.DPtoLP(&m_secondPoint);

			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
			endPt.m_dPX = (double)(m_secondPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			endPt.m_dPY = (double)(m_secondPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

			CLine* theLine = new CLine;
			theLine->Create(begPt,endPt);

			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theLine);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			m_ClickCount = 0;
			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
	}
	else if (pDoc->m_DrawType == 2)//画圆
	{
		if (m_ClickCount == 0)
		{
			CDlgCircleDetail dlgDetail;
			//edit by wjl 2015/06/05
			//暂时屏蔽设置圆形角度和起始角度功能
// 			if (dlgDetail.DoModal() == IDOK)
// 			{
				m_startMadeArc = TRUE;
				m_dStartDegree = 0;
				m_dSweepDegree = 360;
// 				m_dStartDegree = dlgDetail.m_dStartDegree;
// 				m_dSweepDegree = dlgDetail.m_dSweepDegree;
				m_firstPoint = point;
				dc.DPtoLP(&m_firstPoint);
				m_ClickCount ++;
//			}
		}
		else if (m_ClickCount == 1)
		{
			m_secondPoint = point;
			dc.DPtoLP(&m_secondPoint);

			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
			endPt.m_dPX = (double)(m_secondPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			endPt.m_dPY = (double)(m_secondPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

			CCircle* theCCircle = new CCircle;
			theCCircle->Create(begPt, endPt, m_dStartDegree*M_PI/180,m_dSweepDegree*M_PI/180);

			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theCCircle);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			m_startMadeArc = FALSE;
			m_ClickCount = 0;

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
	}
	else if (pDoc->m_DrawType == 3)//画椭圆
	{
		if (m_ClickCount == 0)
		{
			m_firstPoint = point;
			dc.DPtoLP(&m_firstPoint);
			m_ClickCount ++;
			m_bFirstMouseMove = TRUE;
		}
		else if (m_ClickCount == 1)
		{ 
			//固定一个椭圆
			m_dStartDegree = 0;
			m_dSweepDegree = 360;	
			m_ClickCount ++;
			m_secondPoint = point;
			dc.DPtoLP(&m_secondPoint);
			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
			endPt.m_dPX = (double)(m_secondPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			endPt.m_dPY = (double)(m_secondPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;			
			m_dRadiusX = fabs(begPt.m_dPX - endPt.m_dPX);
			m_dRadiusY = fabs(begPt.m_dPY - endPt.m_dPY);
			CPointEx centerPt;
			if ((begPt.m_dPX - endPt.m_dPX) >= 0)
			{
				centerPt.m_dPX = endPt.m_dPX + (begPt.m_dPX - endPt.m_dPX)/2;
			}
			else
			{
				centerPt.m_dPX = begPt.m_dPX + (endPt.m_dPX - begPt.m_dPX)/2;
			}
			if ((begPt.m_dPY - endPt.m_dPY) >= 0)
			{
				centerPt.m_dPY = endPt.m_dPY + (begPt.m_dPY - endPt.m_dPY)/2;
			}
			else
			{
				centerPt.m_dPY = begPt.m_dPY + (endPt.m_dPY - begPt.m_dPY)/2;
			}   

			CEllipse* theEllipse = new CEllipse;
			theEllipse->Create(centerPt, m_dRadiusX/2,m_dRadiusY/2, m_dStartDegree*M_PI/180,m_dSweepDegree*M_PI/180);

			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theEllipse);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);	
			pDoc->m_graphListTop.AddTail(cpBlock);
			m_ClickCount = 0;
			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
	}
	else if (pDoc->m_DrawType == 4) //矩形
	{
		if (m_ClickCount == 0)
		{
			m_firstPoint = point;
			dc.DPtoLP(&m_firstPoint);
			m_ClickCount ++;
			m_bFirstMouseMove = TRUE;
		}
		else if (m_ClickCount == 1)
		{ 
			m_dStartDegree = 0;
			m_dSweepDegree = 360;	
			m_ClickCount ++;
			m_secondPoint = point;
			dc.DPtoLP(&m_secondPoint);

			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;
			endPt.m_dPX = (double)(m_secondPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
			endPt.m_dPY = (double)(m_secondPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;			
			m_dRadiusX = fabs(begPt.m_dPX - endPt.m_dPX);
			m_dRadiusY = fabs(begPt.m_dPY - endPt.m_dPY);
			CPointEx centerPt, tempPtLeftTop, tempPtRightBottom;
			if ((begPt.m_dPX - endPt.m_dPX) >= 0)
			{
				centerPt.m_dPX = endPt.m_dPX + (begPt.m_dPX - endPt.m_dPX)/2;
				tempPtLeftTop.m_dPX = endPt.m_dPX;
				tempPtRightBottom.m_dPX = begPt.m_dPX;
			}
			else
			{
				centerPt.m_dPX = begPt.m_dPX + (endPt.m_dPX - begPt.m_dPX)/2;
				tempPtLeftTop.m_dPX = begPt.m_dPX;
				tempPtRightBottom.m_dPX = endPt.m_dPX;
			}
			if ((begPt.m_dPY - endPt.m_dPY) >= 0)
			{
				centerPt.m_dPY = endPt.m_dPY + (begPt.m_dPY - endPt.m_dPY)/2;
				tempPtRightBottom.m_dPY = endPt.m_dPY;
				tempPtLeftTop.m_dPY = begPt.m_dPY;
			}
			else
			{
				centerPt.m_dPY = begPt.m_dPY + (endPt.m_dPY - begPt.m_dPY)/2;
				tempPtRightBottom.m_dPY = begPt.m_dPY;
				tempPtLeftTop.m_dPY = endPt.m_dPY;
			}

			begPt = tempPtLeftTop;
			endPt = tempPtRightBottom;

			CRectangle* theRectangle = new CRectangle;
			theRectangle->Create(begPt, endPt, centerPt);
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theRectangle);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);	
			pDoc->m_graphListTop.AddTail(cpBlock);

			m_ClickCount = 0;
			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
	}
	else if (pDoc->m_DrawType == 5)//文字
	{
		if (m_ClickCount == 0)
		{
			CText* theText = new CText();
			int width = 0;
			theText->CalTextWidth(&dc, m_InitWinCenterLogicPos, m_iWinAndUserCoorRate, 10, theText->GetFont(), theText->GetText(), width);
			m_firstPoint = point;

			dc.DPtoLP(&m_firstPoint);

			begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x) / m_iWinAndUserCoorRate;
			begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y) / m_iWinAndUserCoorRate;
			endPt.m_dPX = begPt.m_dPX + width / (double)m_iWinAndUserCoorRate;
			endPt.m_dPY = begPt.m_dPY - 10;

			theText->SetHeight(10);
			theText->SetLTpt(begPt);
			theText->Create(begPt, endPt, theText->GetText(), theText->GetFont());
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theText);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);	
			pDoc->m_graphListTop.AddTail(cpBlock);

			m_ClickCount = 0;
			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(), SHOWGRAPHLIST, 0, 0);
		}
		/*
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);

		CEzdFileData* theEzdData = new CEzdFileData;

		begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
		begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

		if (theEzdData->Create(begPt,pDoc) == 0)
		{
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theEzdData);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
		else
		{
			AfxMessageBox(_T("导入ezd文件出错"));
		}
		*/
	}
	else if (pDoc->m_DrawType == 7)
	{
		/*
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);

		CDelay* theDelay = new CDelay;

		begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
		begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

		if (theDelay->Create(begPt,0) == 0)
		{
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theDelay);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
		else
		{
			AfxMessageBox(_T("延时添加有错误"));
		}
		*/
	}
	else if (pDoc->m_DrawType == 8)
	{
		/*
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);

		CInputSignal* theInputSignal = new CInputSignal;

		begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
		begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

		if (theInputSignal->Create(begPt,0,0,0) == 0)
		{
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theInputSignal);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
		else
		{
			AfxMessageBox(_T("输入信号添加有错误"));
		}
		*/
	}
	else if (pDoc->m_DrawType == 9)
	{
		/*
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);

		COutputSignal* theOutputSignal = new COutputSignal;

		begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
		begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

		if (theOutputSignal->Create(begPt,0,0,0) == 0)
		{
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(theOutputSignal);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
		else
		{
			AfxMessageBox(_T("输出信号添加有错误"));
		}
		*/
	}
	else if (pDoc->m_DrawType == 10)	//激光打点
	{		
		m_firstPoint = point;
		dc.DPtoLP(&m_firstPoint);

		CPointLaser* thePointLaser = new CPointLaser();
		begPt.m_dPX = (double)(m_firstPoint.x - m_InitWinCenterLogicPos.x)/m_iWinAndUserCoorRate;
		begPt.m_dPY = (double)(m_firstPoint.y - m_InitWinCenterLogicPos.y)/m_iWinAndUserCoorRate;

		if (thePointLaser->Create(begPt,0) == 0)
		{
			CBlock* cpBlock = NULL;
			CGraph* cpGraph =NULL;
			cpGraph = new CGraph;
			cpGraph->SetGraphType(0);
			cpGraph->AddTail(thePointLaser);
			cpBlock = new CBlock;
			cpBlock->SetBlockType(0);
			cpBlock->AddTail(cpGraph);
			pDoc->m_graphListTop.AddTail(cpBlock);

			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);
		}
		else
		{
			AfxMessageBox(_T("输出信号添加有错误"));
		}
		
	}

	if (pDoc->m_DrawType == 6)
	{
		if (m_ClickCount == 0)
		{
			ScreenToClient(&point);
			m_firstPoint = point;
			dc.DPtoLP(&m_firstPoint);
			m_ClickCount = 1;
		}
	}
	
	CScrollView::OnLButtonDown(nFlags, point);
}

int CMotionControlSoftwareView::DrawCross(CDC* pDC)
{
	CRect rcRect;
	GetClientRect(&rcRect);
	pDC->DPtoLP(&rcRect);

	//十字线
	CPoint screenCenterLeft;
	CPoint screenCenterRight;
	CPoint screenCenterTop;
	CPoint screenCenterBottom;
	//	screenCenterLeft.x = (m_InitWinCenterLogicPos.x)-m_WindowLogicSize.cx/2+ m_RulerEdge;//带标尺
	screenCenterLeft.x = rcRect.left;
	screenCenterLeft.y = m_InitWinCenterLogicPos.y;
	//	screenCenterRight.x = (m_InitWinCenterLogicPos.x)+m_WindowLogicSize.cx/2+ m_RulerEdge;//带标尺
	screenCenterRight.x = rcRect.right;
	screenCenterRight.y = m_InitWinCenterLogicPos.y;

	screenCenterTop.x = m_InitWinCenterLogicPos.x;
	//	screenCenterTop.y = (m_InitWinCenterLogicPos.y)+m_WindowLogicSize.cy/2 - m_RulerEdge;//带标尺
	screenCenterTop.y = rcRect.top;
	screenCenterBottom.x = m_InitWinCenterLogicPos.x;
	//	screenCenterBottom.y = (m_InitWinCenterLogicPos.y)-m_WindowLogicSize.cy/2 - m_RulerEdge;//带标尺
	screenCenterBottom.y = rcRect.bottom;
	pDC->MoveTo(screenCenterLeft);
	pDC->LineTo(screenCenterRight);
	pDC->MoveTo(screenCenterTop);
	pDC->LineTo(screenCenterBottom);
	return 0;
}

int CMotionControlSoftwareView::DrawRuler(CDC* pDC)
{
	OnPrepareDC(pDC);
	GetClientRect(&m_ClientRect);
	CRect dClientRectView = m_ClientRect;
	//设定XY标尺基线坐标
	CPoint pBegRuler = CPoint(25,25);
	CPoint pEndRuler = CPoint(dClientRectView.right+25,dClientRectView.bottom+25);
	m_begRulerPtX = m_begRulerPtY = pBegRuler;
	m_endRulerPtX.x = pEndRuler.x;
	m_endRulerPtX.y = pBegRuler.y;
	m_endRulerPtY.x = pBegRuler.x;
	m_endRulerPtY.y = pEndRuler.y;
	pDC->DPtoLP(&m_begRulerPtX);
	pDC->DPtoLP(&m_endRulerPtX);
	pDC->DPtoLP(&m_begRulerPtY);
	pDC->DPtoLP(&m_endRulerPtY);

	//画X,Y方向标尺基线
	int num = 0;
	pDC->MoveTo(m_begRulerPtX);
	pDC->LineTo(m_endRulerPtX);
	pDC->MoveTo(m_begRulerPtY);
	pDC->LineTo(m_endRulerPtY);

	//画X、Y方向灰色区
	CBrush brush(RGB(215,215,215));
	CRect rect1(0,0,m_endRulerPtX.x,m_endRulerPtX.y);
	pDC->FillRect(&rect1,&brush);
	CRect rect2(0,0,m_endRulerPtY.x,m_endRulerPtY.y);
	pDC->FillRect(&rect2,&brush);
	 
	CFont fontRow;
	fontRow.CreateFont(
	 	30,                        // nHeight
	 	10,                        // nWidth
	 	0,                         // nEscapement
	 	0,                         // nOrientation
	 	FW_NORMAL,                 // nWeight
	 	FALSE,                     // bItalic
	 	FALSE,                     // bUnderline
	 	0,                         // cStrikeOut
	 	ANSI_CHARSET,              // nCharSet
	 	OUT_DEFAULT_PRECIS,        // nOutPrecision
	 	CLIP_DEFAULT_PRECIS,       // nClipPrecision
	 	DEFAULT_QUALITY,           // nQuality
	 	DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	 	_T("Arial"));                 // lpszFacename
	CFont* pOldFont;
	
	CPointEx begRulerExX,endRulerExX,begRulerExY,endRulerExY;
	begRulerExX = LPointToMyPoint(m_begRulerPtX);
	endRulerExX = LPointToMyPoint(m_endRulerPtX);
	begRulerExY = LPointToMyPoint(m_begRulerPtY);
	endRulerExY = LPointToMyPoint(m_endRulerPtY);
	double dEx = (endRulerExX.m_dPX-begRulerExX.m_dPX)/150.0;
	double dExMinPuleValue = GetMinRuleValue(dEx);
	
	pOldFont = pDC->SelectObject(&fontRow);
	pDC->SetTextAlign(TA_CENTER);
	//X方向标尺处理
	DrawPuleLineX(pDC,m_begRulerPtX,m_endRulerPtX,dExMinPuleValue,m_endRulerPtY);

	CFont cfontCol;
	cfontCol.CreateFont(
	 	30,                        // nHeight
	 	10,                        // nWidth
	 	-900,                       // nEscapement
	 	0,                         // nOrientation
	 	FW_NORMAL,                 // nWeight
	 	FALSE,                     // bItalic
	 	FALSE,                     // bUnderline
	 	0,                         // cStrikeOut
	 	ANSI_CHARSET,              // nCharSet
	 	OUT_DEFAULT_PRECIS,        // nOutPrecision
	 	CLIP_DEFAULT_PRECIS,       // nClipPrecision
	 	DEFAULT_QUALITY,           // nQuality
	 	DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	 	_T("Arial"));                 // lpszFacename
	pDC->SelectObject(&cfontCol);
	pDC->SetTextAlign(TA_CENTER);
	//Y方向标尺
	DrawPuleLineY(pDC,m_begRulerPtY,m_endRulerPtY,dExMinPuleValue,m_endRulerPtX);

	pDC->SelectObject(pOldFont);

	return 0;
}

int CMotionControlSoftwareView::DrawGrid(CDC* pDC)
{
	int num = 0;
	//横线
	//(Y方向幅面,横向线的数量)
	num=(int)(m_dAxisY/m_gridSize);
	CPointEx leftFirstPt;	//左边点
	CPointEx rightFirstPt;	//右边点
	leftFirstPt.m_dPX = m_viewMath.make_int((m_InitWinCenterLogicPos.x)-m_dAxisX*m_iWinAndUserCoorRate/2.0);
	leftFirstPt.m_dPY = m_InitWinCenterLogicPos.y+m_dAxisY*m_iWinAndUserCoorRate/2.0;
	rightFirstPt.m_dPX = m_viewMath.make_int((m_InitWinCenterLogicPos.x)+m_dAxisX*m_iWinAndUserCoorRate/2.0);
	rightFirstPt.m_dPY = leftFirstPt.m_dPY;
	for(int i=0;i<=num;i++)
	{
		CPoint leftPt, rightPt;
		leftPt.x = (int)leftFirstPt.m_dPX;
		leftPt.y = m_viewMath.make_int(leftFirstPt.m_dPY-i*m_gridSize*m_iWinAndUserCoorRate);
		rightPt.x = (int)rightFirstPt.m_dPX;
		rightPt.y = leftPt.y;
		if (leftPt.y == m_InitWinCenterLogicPos.y)//中心线不用画
		{
			continue;
		}
		pDC->MoveTo(leftPt);
		pDC->LineTo(rightPt);
	}

	//竖线
	//(X方向幅面,竖向线的数量)
	num=(int)(m_dAxisX/m_gridSize);
	CPointEx topFirstPt;	//上边点
	CPointEx bottomFirstPt; //下边点
	topFirstPt.m_dPX =  m_InitWinCenterLogicPos.x-m_dAxisX*m_iWinAndUserCoorRate/2.0;
	topFirstPt.m_dPY = m_viewMath.make_int((m_InitWinCenterLogicPos.y)+m_dAxisY*m_iWinAndUserCoorRate/2.0);
	bottomFirstPt.m_dPX = topFirstPt.m_dPX;
	bottomFirstPt.m_dPY = m_viewMath.make_int((m_InitWinCenterLogicPos.y)-m_dAxisY*m_iWinAndUserCoorRate/2.0);
	for(int k=0; k<=num; k++)
	{
		CPoint topPt, bottomPt;
		topPt.x = m_viewMath.make_int(topFirstPt.m_dPX + k*m_gridSize*m_iWinAndUserCoorRate);
		topPt.y = (int)topFirstPt.m_dPY;
		bottomPt.x = topPt.x;
		bottomPt.y = (int)bottomFirstPt.m_dPY;
		if (topPt.x == m_InitWinCenterLogicPos.x)//中心线不用画
		{
			continue;
		}
		pDC->MoveTo(topPt);
		pDC->LineTo(bottomPt);
	}
	return 0;
}

int CMotionControlSoftwareView::DrawWorkArea(CDC* pDC)
{
	//画坐标系区域方框
	int x1,x2,y1,y2;
	CPoint leftTop, rightTop, leftBottom, rightBottom;
	x1=m_viewMath.make_int((m_InitWinCenterLogicPos.x)-m_dAxisX*m_iWinAndUserCoorRate/2.0);
	x2=m_viewMath.make_int((m_InitWinCenterLogicPos.x)+m_dAxisX*m_iWinAndUserCoorRate/2.0);
	y2=m_viewMath.make_int((m_InitWinCenterLogicPos.y)-m_dAxisY*m_iWinAndUserCoorRate/2.0);
	y1=m_viewMath.make_int((m_InitWinCenterLogicPos.y)+m_dAxisY*m_iWinAndUserCoorRate/2.0);
	leftTop.x = x1;
	leftTop.y = y1;
	rightTop.x = x2;
	rightTop.y = y1;
	leftBottom.x = x1;
	leftBottom.y = y2;
	rightBottom.x = x2;
	rightBottom.y = y2;

	//画下右灰色区
	CBrush brush(RGB(192,192,192));
	CRect rect1(leftBottom.x+30,leftBottom.y,rightBottom.x+30,rightBottom.y-30);
	pDC->FillRect(&rect1,&brush);
	CRect rect2(rightTop.x,rightTop.y-30,rightBottom.x+30,rightBottom.y-30);
	pDC->FillRect(&rect2,&brush);
	//画工作区域框
	pDC->MoveTo(leftTop);               
	pDC->LineTo(rightTop);
	pDC->LineTo(rightBottom);
	pDC->LineTo(leftBottom);
	pDC->LineTo(leftTop);
	return 0;
}

int CMotionControlSoftwareView::SetBasicalCoordinateSys(CMotionControlSoftwareDoc* pDoc, CDC* pDC)
{
	OnPrepareDC(pDC);
	GetClientRect(&m_ClientRect);
	CRect dClientRectView = m_ClientRect;

	CDocTemplate* pTemp = pDoc->GetDocTemplate();
	POSITION pos = pTemp->GetFirstDocPosition();
	CDocument* pFirstDoc = pTemp->GetNextDoc(pos);
	int iFirst = 0;
	if (pFirstDoc == pDoc)
	{
		iFirst = 2;//第一个文档
	}
	else
	{
		iFirst = 1;//第个文档
	}

	if (m_ClientRect.IsRectNull())
	{
		return -1;
	}

	//if (m_iOnDraw == iFirst) 
	if ((m_bFirstRun)||(m_iOnDraw == iFirst))
	{
		//设置窗口的中心为32000,-32000，左上角坐标为:(32000-(屏幕宽度一半)),(-32000-(屏幕高度的一半))
		pDC->DPtoLP(&m_ClientRect);
		m_WindowLogicSize.cx = m_ClientRect.Width();
		m_WindowLogicSize.cy = m_ClientRect.Height();
		m_WindowWorkAreLogicSize.cx = m_WindowLogicSize.cx - m_RulerEdge*2;
		m_WindowWorkAreLogicSize.cy = m_WindowLogicSize.cy + m_RulerEdge*2;
		m_InitWinCenterLogicPos.x = MAX_SIZE/2;  
		m_InitWinCenterLogicPos.y = -MAX_SIZE/2;
		m_InitWinLeftTopLogicPos.x = m_InitWinCenterLogicPos.x-m_WindowLogicSize.cx/2;
		m_InitWinLeftTopLogicPos.y = m_InitWinCenterLogicPos.y-m_WindowLogicSize.cy/2;
		m_WinLeftTopLogicPos = m_InitWinLeftTopLogicPos;
		ScrollToPosition(m_InitWinLeftTopLogicPos);

		m_dAxisX = pDoc->m_param.m_viewPara.m_dAxisX;
		m_dAxisY = pDoc->m_param.m_viewPara.m_dAxisY;
		int iRate = 1;
		if(abs(m_WindowWorkAreLogicSize.cx)<abs(m_WindowWorkAreLogicSize.cy))
		{
			iRate = (int)((m_WindowWorkAreLogicSize.cx)/m_dAxisX);
		}
		else
		{
			iRate = (int)((m_WindowWorkAreLogicSize.cy)/m_dAxisY);
		}
		m_iInitWinAndUserCoorRate = abs(iRate);
		m_iWinAndUserCoorRate = m_iInitWinAndUserCoorRate;
		m_begRulerPtX.x = m_InitWinLeftTopLogicPos.x + m_RulerEdge;
		m_begRulerPtX.y = m_InitWinLeftTopLogicPos.y - m_RulerEdge;
		m_endRulerPtX.x = m_InitWinLeftTopLogicPos.x + m_WindowLogicSize.cx - m_RulerEdge;
		m_endRulerPtX.y = m_begRulerPtX.y;
		m_begRulerPtY = m_begRulerPtX;
		m_endRulerPtY.x = m_begRulerPtX.x;
		m_endRulerPtY.y = m_begRulerPtY.y + m_WindowLogicSize.cy + m_RulerEdge;
		m_WinCenterLogicPos = m_InitWinCenterLogicPos;

		m_bFirstRun = FALSE;
	}
	else
	{
		if (m_iZoomRate != m_iOldZoomRate || m_CenterMove != m_oldCenterMove)
		{
			CRect rect;
			GetClientRect(&rect);
			pDC->DPtoLP(&rect);

			m_iOldZoomRate = m_iZoomRate;
			m_dNOMultiple = pow(1.2,(double)(m_iZoomRate));
			m_iWinAndUserCoorRate = m_iInitWinAndUserCoorRate * m_dNOMultiple;
			m_WinCenterLogicPos.x += m_CenterMove.cx;
			m_WinCenterLogicPos.y += m_CenterMove.cy;
			m_WinLeftTopLogicPos.x += m_CenterMove.cx;
			m_WinLeftTopLogicPos.y += m_CenterMove.cy;
			ScrollToPosition(m_WinLeftTopLogicPos);
			m_oldCenterMove = m_CenterMove;
		}
	}

	CPen *newPen,*oldPen;

	newPen = new CPen(PS_SOLID,1,RGB(0,0,0));
	oldPen = pDC->SelectObject(newPen);	
	DrawRuler(pDC);
	pDC->SelectObject(oldPen);
	delete newPen;

	newPen = new CPen(PS_SOLID,1,RGB(0,0,0));
	oldPen=pDC->SelectObject(newPen);	
	DrawWorkArea(pDC);
	delete newPen;

	newPen=new CPen(PS_DASHDOTDOT,1,RGB(0,0,255)); 
	oldPen=pDC->SelectObject(newPen);	
	DrawCross(pDC);	
	pDC->SelectObject(oldPen);
	delete newPen;

	//画选中框
	ShowSelectedRect(pDC);

	return 0;
}

int CMotionControlSoftwareView::DrawRectOutLine(CRect crOutLine, int iType)
{
// 	CRect crLeftTop, crRightTop, crLeftBottom, crRightBottom;
// 	CPoint cpLeftTop, cpRightTop, cpLeftBottom, cpRightBottom;
// 
// 	cpLeftTop.x = crOutLine.left;
// 	cpLeftTop.y = crOutLine.top;
// 
// 	cpRightTop.x = crOutLine.right;
// 	cpRightTop.y = crOutLine.top;
// 
// 	cpLeftBottom.x = crOutLine.left;
// 	cpLeftBottom.y = crOutLine.bottom;
// 
// 	cpRightBottom.x = crOutLine.right;
// 	cpRightBottom.y = crOutLine.bottom;
// 
// 	gpWorkData->CalBlackBlock(crLeftTop,cpLeftTop);
// 	gpWorkData->CalBlackBlock(crRightTop,cpRightTop);
// 	gpWorkData->CalBlackBlock(crLeftBottom,cpLeftBottom);
// 	gpWorkData->CalBlackBlock(crRightBottom,cpRightBottom);
// 
// 	if (iType == 1)
// 	{
// 		CBrush brs(RGB(0,255,255));
// 		CBrush* pOldbrs = m_pDC->SelectObject(&brs);
// 		m_pDC->FillRect(&crLeftTop,&brs);
// 		m_pDC->FillRect(&crRightTop,&brs);
// 		m_pDC->FillRect(&crLeftBottom,&brs);
// 		m_pDC->FillRect(&crRightBottom,&brs);
// 		m_pDC->SelectObject(pOldbrs);
// 	}
// 	else if (iType == 0)
// 	{
// 		CBrush brs(RGB(255,255,255));
// 		CBrush* pOldbrs = m_pDC->SelectObject(&brs);
// 		m_pDC->FillRect(&crLeftTop,&brs);
// 		m_pDC->FillRect(&crRightTop,&brs);
// 		m_pDC->FillRect(&crLeftBottom,&brs);
// 		m_pDC->FillRect(&crRightBottom,&brs);
// 		m_pDC->SelectObject(pOldbrs);
// 	}
	return 0;
}


BOOL CMotionControlSoftwareView::CheckPtInRect(CRect rectZone, CPoint selectPt)
{
	BOOL bRet = FALSE;
	if ((selectPt.x >= rectZone.left) && (selectPt.x <= rectZone.right) 
		&& (selectPt.y >= rectZone.bottom) && (selectPt.y <= rectZone.top))
	{
		bRet = TRUE;
	}
	return bRet;
}

int CMotionControlSoftwareView::CalTriangle(POINT* trianglePoint, int iPointLen,int iDirect,int iArrowLen)
{
	/*
	double dChangeLen = tan(30*PI/180) * iArrowLen;
	if (iDirect == 1)//上
	{		
		trianglePoint[1].x = trianglePoint[0].x-dChangeLen;
		trianglePoint[1].y = trianglePoint[0].y-iArrowLen;
		trianglePoint[2].x = trianglePoint[0].x+dChangeLen;
		trianglePoint[2].y = trianglePoint[0].y-iArrowLen;
	}
	else if (iDirect == 2)//下
	{	
		trianglePoint[1].x = trianglePoint[0].x-dChangeLen;
		trianglePoint[1].y = trianglePoint[0].y+iArrowLen;
		trianglePoint[2].x = trianglePoint[0].x+dChangeLen;
		trianglePoint[2].y = trianglePoint[0].y+iArrowLen;
	}
	else if (iDirect == 3)//左
	{
		trianglePoint[1].x = trianglePoint[0].x+iArrowLen;
		trianglePoint[1].y = trianglePoint[0].y+dChangeLen;
		trianglePoint[2].x = trianglePoint[0].x+iArrowLen;
		trianglePoint[2].y = trianglePoint[0].y-dChangeLen;
	}
	else if (iDirect == 4)//右
	{
		trianglePoint[1].x = trianglePoint[0].x-iArrowLen;
		trianglePoint[1].y = trianglePoint[0].y+dChangeLen;
		trianglePoint[2].x = trianglePoint[0].x-iArrowLen;
		trianglePoint[2].y = trianglePoint[0].y-dChangeLen;
	}
	trianglePoint[3].x = trianglePoint[0].x;
	trianglePoint[3].y = trianglePoint[0].y;
	*/
	return 0;
}
int CMotionControlSoftwareView::SetColor(CDC** pDC, int iColorLay)
{
	CPen pRed(PS_SOLID,0,RGB(255,0,0));
	CPen pBlue(PS_SOLID,0,RGB(0,0,255));
	CPen pBlack(PS_SOLID,0,RGB(0,0,0));
	CPen pGreen(PS_SOLID,0,RGB(0,255,0));
	CPen pMagenta(PS_SOLID,0,RGB(255,192,203));
	CPen pPurple(PS_SOLID,0,RGB(128,0,128));
	CPen pCYAN(PS_SOLID,0,RGB(127,255,212));
	CPen pYellow(PS_SOLID,0,RGB(255,255,0));

	if (iColorLay==0)
	{
		(*pDC)->SelectObject(&pBlack);
	}
	else if (iColorLay==1)
	{
		(*pDC)->SelectObject(&pRed);
	}
	else if (iColorLay==2)
	{
		(*pDC)->SelectObject(&pBlue);
	}
	else if (iColorLay==3)
	{
		(*pDC)->SelectObject(&pYellow);
	}
	else if (iColorLay==4)
	{
		(*pDC)->SelectObject(&pMagenta);
	}
	else if (iColorLay==5)
	{
		(*pDC)->SelectObject(&pGreen);
	}
	else if (iColorLay==6)
	{
		(*pDC)->SelectObject(&pCYAN);
	}
	else if (iColorLay==7)
	{
		(*pDC)->SelectObject(&pPurple);
	}
	return 0;
}

int CMotionControlSoftwareView::DrawAllGraph(CMotionControlSoftwareDoc* pDoc,CDC* pDC)
{
	int iCount = pDoc->m_graphListTop.GetListAllCount();
	if (iCount !=0)
	{
		iCount = 5;
	}
	else
	{
		return 0;
	}
	for (std::vector<CBlock*>::iterator it=pDoc->m_graphListTop.begin(); it!=pDoc->m_graphListTop.end(); it++)
	{
		CBlock* pCurBlock = (CBlock*)(*it);
		CPen* pOldPen = NULL;
		CPen pRed(PS_SOLID,0,RGB(255,0,0));
		CPen pBlue(PS_SOLID,0,RGB(0,0,255));
		CPen pBlack(PS_SOLID,0,RGB(0,0,0));
		CPen pGreen(PS_SOLID,0,RGB(0,128,0));
		CPen pMagenta(PS_SOLID,0,RGB(255,0,255));
		CPen pPurple(PS_SOLID,0,RGB(128,0,128));
		CPen pCYAN(PS_SOLID,0,RGB(0,255,255));
		CPen pYellow(PS_SOLID,0,RGB(255,255,0));
		CPen pSelectColor(PS_DOT,0,RGB(0,128,255));

		for (std::vector<CGraph*>::iterator it=pCurBlock->begin(); it!=pCurBlock->end(); it++)
		{
			CGraph* pCurGraph = (CGraph*)(*it);
			CElement* pFirstElement = pCurGraph->GetHead();
			CElement* pLastElement = pCurGraph->GetTail();
			int iGraph = pCurGraph->GetListAllCount();
			for (std::vector<CElement*>::iterator it=pCurGraph->begin(); it!=pCurGraph->end(); it++)
			{
				CElement*pCurElement = (CElement*)(*it);
				if (pCurGraph->m_bLeadInLine && (pCurElement == pFirstElement))
				{
					//如果第一个是引入线则直接不绘图
					continue;
				}
				//引入引出线
				if (pCurElement==pLastElement)
				{
					if (pCurGraph->m_bLeadInLine)
					{
						pDC->SelectObject(&pRed);
						int iType = pFirstElement->m_iElementType;
						CPoint cCurLinePtBeg,cCurLinePtEnd,cCurLinePtCenter;				
						if (iType == 0)//直线
						{
							CLine* pLine = (CLine*)pFirstElement;
							pLine->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
						}
						else if (iType == 1)//圆
						{
							CCircle* pArc = (CCircle*)pFirstElement;
							pArc->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
						}
					}
					if ((pCurGraph->m_bLeadOutLine))
					{
						pDC->SelectObject(&pBlue);
						int iType = pLastElement->m_iElementType;
						CPoint cCurLinePtBeg,cCurLinePtEnd,cCurLinePtCenter;				
						if (iType == 0)//直线
						{
							CLine* pLine = (CLine*)pLastElement;
							pLine->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
						}
						else if (iType == 1)//圆
						{
							CCircle* pArc = (CCircle*)pLastElement;
							pArc->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
						}
						continue;
					}
				}
				
				//非引入引出线的处理
				if (pCurBlock->m_bSelected)
				{
					pDC->SelectObject(&pSelectColor);
				}
				else
				{
					if (pCurGraph->m_iColorLay==0)
					{
						pDC->SelectObject(&pBlack);
					}
					else if (pCurGraph->m_iColorLay==1)
					{
						pDC->SelectObject(&pRed);
					}
					else if (pCurGraph->m_iColorLay==2)
					{
						pDC->SelectObject(&pYellow);
					}
					else if (pCurGraph->m_iColorLay==3)
					{
						pDC->SelectObject(&pGreen);
					}
					else if (pCurGraph->m_iColorLay==4)
					{
						pDC->SelectObject(&pCYAN);
					}
					else if (pCurGraph->m_iColorLay==5)
					{
						pDC->SelectObject(&pBlue);
					}
					else if (pCurGraph->m_iColorLay==6)
					{
						pDC->SelectObject(&pMagenta);
					}
					else if (pCurGraph->m_iColorLay==7)
					{
						pDC->SelectObject(&pPurple);
					}
				}
				//画图形
				int iType = pCurElement->m_iElementType;
				CPoint cCurLinePtBeg,cCurLinePtEnd,cCurLinePtCenter;				
				if (iType == 0)//直线
				{
					CLine* pLine = (CLine*)pCurElement;
					pLine->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 1)//圆
				{
					CCircle* pArc = (CCircle*)pCurElement;
					pArc->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 2)//椭圆
				{
					CEllipse* pArc = (CEllipse*)pCurElement;
					pArc->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 3)//Text
				{
					CText* pText = (CText*)pCurElement;
					pText->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 4)//样条曲线
				{
					CSpline* pSpline = (CSpline*)pCurElement;
					pSpline->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 5)//矩形
				{
					CRectangle* pRectangle = (CRectangle*)pCurElement;
					pRectangle->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
				else if (iType == 9) //点
				{
					CPointLaser* pPLaser = (CPointLaser*)pCurElement;
					pPLaser->Draw(pDC,m_InitWinCenterLogicPos,m_iWinAndUserCoorRate);
				}
			}
		}
	}	
	return 0;
}

void CMotionControlSoftwareView::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
	// TODO: Add your message handler code here and/or call default
	//     CSize sizeClient;
	//     CSize sizeSb;
	//     GetTrueClientSize(sizeClient, sizeSb);
	// 	
	//     BOOL hasHorzBar(FALSE);
	//     BOOL hasVertBar(FALSE);
	// 	int iSizeX = lpncsp->rgrc[0].bottom;
	// 	int iSizeY = lpncsp->rgrc[0].right;
	// 	int iSizeScrollX = GetSystemMetrics(SM_CYHSCROLL);
	// 	int iSizeScrollY = GetSystemMetrics(SM_CXHSCROLL);
	// 	lpncsp->rgrc[0].bottom += GetSystemMetrics(SM_CYHSCROLL)*2-sizeSb.cy+3;
	// 	lpncsp->rgrc[0].right += GetSystemMetrics(SM_CXHSCROLL)*2-sizeSb.cx+3;
	CScrollView::OnNcCalcSize(bCalcValidRects, lpncsp);
}

BOOL CMotionControlSoftwareView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: Add your message handler code here and/or call default
	//	TODO: Add your command handler code here
	CMotionControlSoftwareDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

		int iOldZoomRate = m_iZoomRate;

	if (zDelta > 0)
	{
		if (m_iZoomRate <= 50)
		{
			m_iZoomRate += 1;
		}
		else
		{
			return FALSE;
		}
	}
	else
	{
		if (m_iZoomRate >= -30)
		{
			m_iZoomRate -= 1;
		}
		else
		{
			return FALSE;
		}
	}

	CClientDC dc(this);
	CRect crClientRect;
	OnPrepareDC(&dc);
	GetClientRect(&crClientRect);
	CPoint centerPt = crClientRect.CenterPoint();
	ScreenToClient(&pt);
	dc.DPtoLP(&pt);
	dc.DPtoLP(&centerPt);

	m_CenterMove.cx = 0;
	m_CenterMove.cy = 0;

	//m_CenterMove偏移距离计算
	double dOldMultiple = pow(1.2,(double)(iOldZoomRate));
	double dNewMultiple = pow(1.2,(double)(m_iZoomRate));

	double dX = ((double)(pt.x-m_InitWinCenterLogicPos.x))/(dOldMultiple*(double)m_iInitWinAndUserCoorRate);
	double dY = ((double)(pt.y-m_InitWinCenterLogicPos.y))/(dOldMultiple*(double)m_iInitWinAndUserCoorRate);

	int iMovePosX = dX*(dNewMultiple-dOldMultiple)*m_iInitWinAndUserCoorRate;
	int iMovePosY = dY*(dNewMultiple-dOldMultiple)*m_iInitWinAndUserCoorRate;

	m_CenterMove.cx = iMovePosX;
	m_CenterMove.cy = iMovePosY;

	//Invalidate(TRUE);
	CRect rect;
	GetClientRect(rect);
	InvalidateRect(rect);
	SetBasicalCoordinateSys(pDoc,&dc);
	DrawAllGraph(pDoc,&dc);
}

void CMotionControlSoftwareView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
	// TODO: Add your specialized code here and/or call the base class
	m_bActive = TRUE;
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}


void CMotionControlSoftwareView::OnActivateFrame(UINT nState, CFrameWnd* pDeactivateFrame)
{
	// TODO: Add your specialized code here and/or call the base class

	CScrollView::OnActivateFrame(nState, pDeactivateFrame);
}


void CMotionControlSoftwareView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
	// TODO: Add your specialized code here and/or call the base class
	Invalidate();
}


BOOL CMotionControlSoftwareView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
	// TODO: Add your specialized code here and/or call the base class

	return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
}


void CMotionControlSoftwareView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	Invalidate(TRUE);

	CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CMotionControlSoftwareView::OnButtonZoomout(CMotionControlSoftwareDoc* pDoc)
{
	pDoc->m_DrawType = 0;
	ASSERT_VALID(pDoc);

	if (m_iZoomRate <= 50)
	{
		m_iZoomRate +=1;
	}
	CClientDC dc(this);
	CRect crClientRect;
	OnPrepareDC(&dc);
	GetClientRect(&crClientRect);
	CPoint pCenter = crClientRect.CenterPoint();
	dc.DPtoLP(&pCenter);
	if (pDoc->m_graphSelectedList.GetCount() == 0)
	{
		m_CenterMove.cx = 0;
		m_CenterMove.cy = 0;
	}
	else
	{
		CRectEx rectSelected;
		if (pDoc->GetSelectedRect(rectSelected) == 0)
		{
			int iCurRate = m_iInitWinAndUserCoorRate*pow(1.2,m_iZoomRate);
			int iDesCenterX = (int)(rectSelected.GetCenterPt().m_dPX*iCurRate+m_InitWinCenterLogicPos.x);
			int iDesCenterY = (int)(rectSelected.GetCenterPt().m_dPY*iCurRate+m_InitWinCenterLogicPos.y);
			m_CenterMove.cx = iDesCenterX - m_WinCenterLogicPos.x;
			m_CenterMove.cy = iDesCenterY - m_WinCenterLogicPos.y;
		}
		else
		{
			m_iZoomRate --;
		}
	}
	// 	Invalidate(TRUE);
	// 	SetBasicalCoordinateSys(pDoc,&dc);
	// 	DrawAllGraph(pDoc,&dc);
}

void CMotionControlSoftwareView::OnButtonZoomin(CMotionControlSoftwareDoc* pDoc)
{
	//TODO: Add your command handler code here
	//TODO: Add your command handler code here
	pDoc->m_DrawType = 0;
	ASSERT_VALID(pDoc);

	if (m_iZoomRate >= -50)
	{
		m_iZoomRate -=1;
	}

	CClientDC dc(this);
	CRect crClientRect;
	OnPrepareDC(&dc);
	GetClientRect(&crClientRect);    
	CPoint pCenter = crClientRect.CenterPoint();
	dc.DPtoLP(&pCenter);

	if (pDoc->m_graphSelectedList.GetCount() == 0)
	{
		m_CenterMove.cx = 0;
		m_CenterMove.cy = 0;
	}
	else
	{
		CRectEx rectSelected;
		if (pDoc->GetSelectedRect(rectSelected) == 0)
		{
			int iCurRate = m_iInitWinAndUserCoorRate*pow(1.2,m_iZoomRate);
			int iDesCenterX = (int)(rectSelected.GetCenterPt().m_dPX*iCurRate+m_InitWinCenterLogicPos.x);
			int iDesCenterY = (int)(rectSelected.GetCenterPt().m_dPY*iCurRate+m_InitWinCenterLogicPos.y);
			m_CenterMove.cx = iDesCenterX - m_WinCenterLogicPos.x;
			m_CenterMove.cy = iDesCenterY - m_WinCenterLogicPos.y;
		}
		else
		{
			m_iZoomRate ++;
		}
	}
	// 	Invalidate(TRUE);
	// 	SetBasicalCoordinateSys(pDoc,&dc);
	// 	DrawAllGraph(pDoc,&dc);
}

void CMotionControlSoftwareView::OnButtonZoofull(CMotionControlSoftwareDoc* pDoc)
{
	pDoc->m_DrawType = 0;
	ASSERT_VALID(pDoc);

	CClientDC dc(this);
	OnPrepareDC(&dc);
	GetClientRect(&m_ClientRect);
	dc.DPtoLP(&m_ClientRect);

	CRectEx rectSelected;
	if (pDoc->GetSelectedRect(rectSelected) == 0)
	{
		double dWidth = rectSelected.GetWidth();
		double dHeight = rectSelected.GetHeight();		

		int iRate = 1;
		int dRateX = (int)((double)(abs(m_ClientRect.Width())-50)/dWidth);
		dRateX = abs(dRateX);
		int dRateY = (int)((double)(abs(m_ClientRect.Height())-50)/dHeight);
		dRateY = abs(dRateY);
		if(dRateX<dRateY)
		{
			iRate = dRateX;
		}
		else
		{
			iRate = dRateY;
		}

		double dRate = (double)iRate / (double)m_iInitWinAndUserCoorRate;

/*		if (dRate <= 1)
		{
			dRate = 1;
		}
		else
		{
			dRate = dRate / m_iInitWinAndUserCoorRate;
		}
*/
		m_iZoomRate = GetNearestZoomRate(dRate);

		double dTempRate = pow(1.2,m_iZoomRate);
		if (dTempRate > dRate)
		{
			m_iZoomRate -- ;
		}

		int iCurRate = m_iInitWinAndUserCoorRate*pow(1.2,m_iZoomRate);
		int iDesCenterX = (int)(rectSelected.GetCenterPt().m_dPX*iCurRate+m_InitWinCenterLogicPos.x);
		int iDesCenterY = (int)(rectSelected.GetCenterPt().m_dPY*iCurRate+m_InitWinCenterLogicPos.y);
		m_CenterMove.cx = iDesCenterX - m_WinCenterLogicPos.x;
		m_CenterMove.cy = iDesCenterY - m_WinCenterLogicPos.y;

	}

	Invalidate(TRUE);

}

void CMotionControlSoftwareView::OnButtonZoomfile(CMotionControlSoftwareDoc* pDoc)
{
	// TODO: Add your command handler code here
	//	for (int i=0; i<2; i++)
	//	{
	// 		CDocTemplate* pTemp = pDoc->GetDocTemplate();
	// 		POSITION pos = pTemp->GetFirstDocPosition();
	// 		CDocument* pFirstDoc = pTemp->GetNextDoc(pos);
	// 		int iFirst = 0;
	// 		if (pFirstDoc == pDoc)
	// 		{
	// 			m_iOnDraw = 1;//第一个文档
	// 		}
	// 		else
	// 		{
	// 			m_iOnDraw = 0;
	// 		}
	//		m_bFirstRun = TRUE;

	m_iOnDraw = 1;
	m_iZoomRate = 0;
	m_LeftTopMove.cx = 0;
	m_LeftTopMove.cy = 0;
	m_CenterMove.cx = 0;
	m_CenterMove.cy = 0;
	pDoc->m_DrawType = 0;
	// 	Invalidate(TRUE);
	// 	//	}
	// 	CClientDC dc(this);
	// 	OnPrepareDC(&dc);
	// 	SetBasicalCoordinateSys(pDoc,&dc);
	// 	DrawAllGraph(pDoc,&dc);
}


void CMotionControlSoftwareView::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	CScrollView::OnClose();
}


void CMotionControlSoftwareView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
	// TODO: Add your specialized code here and/or call the base class
	GetClientRect(&m_ClientRect);
	pDC->SetMapMode(MM_TEXT);
	pDC->SetViewportOrg(m_ClientRect.Width()/2,m_ClientRect.Height()/2);
	CScrollView::OnPrepareDC(pDC, pInfo);
}

CPointEx CMotionControlSoftwareView::LPointToMyPoint(CPoint pointMove)
{
	CPointEx exMyPoint;

	exMyPoint.m_dPX = ((double)(pointMove.x-m_InitWinCenterLogicPos.x))/((double)m_iWinAndUserCoorRate);
	exMyPoint.m_dPY = ((double)(pointMove.y-m_InitWinCenterLogicPos.y))/((double)m_iWinAndUserCoorRate);

	return exMyPoint;
}

CPoint CMotionControlSoftwareView::MyPointToLPoint(CPointEx pointMove)
{
	CPoint lPoint;

	lPoint.x = pointMove.m_dPX*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.x;
	lPoint.y = pointMove.m_dPY*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.y;

	return lPoint;
}

//获取计算出最小标尺单位距离
double CMotionControlSoftwareView::GetMinRuleValue(double dEx)
{
	double dMinPuleValue = 1.0;
	if(dEx<=0.01)
	{
		dMinPuleValue = 0.01;
	}
	else if((dEx>0.01)&&(dEx<=0.02))
	{
		dMinPuleValue = 0.02;
	}
	else if((dEx>0.01)&&(dEx<=0.05))
	{
		dMinPuleValue = 0.05;
	}
	else if((dEx>0.01)&&(dEx<=0.1))
	{
		dMinPuleValue = 0.1;
	}
	else if((dEx>0.01)&&(dEx<=0.2))
	{
		dMinPuleValue = 0.2;
	}
	else if((dEx>0.01)&&(dEx<=0.5))
	{
		dMinPuleValue = 0.5;
	}
	else if((dEx>0.01)&&(dEx<=1))
	{
		dMinPuleValue = 1;
	}
	else if((dEx>0.01)&&(dEx<=2))
	{
		dMinPuleValue = 2;
	}
	else if((dEx>0.01)&&(dEx<=4))
	{
		dMinPuleValue = 4;
	}
	else if((dEx>0.01)&&(dEx<=8))
	{
		dMinPuleValue = 8;
	}
	else if((dEx>0.01)&&(dEx<=16))
	{
		dMinPuleValue = 16;
	}
	else if((dEx>0.01)&&(dEx<=32))
	{
		dMinPuleValue = 32;
	}
	else if((dEx>0.01)&&(dEx<=64))
	{
		dMinPuleValue = 64;
	}
	else if((dEx>0.01)&&(dEx<=128))
	{
		dMinPuleValue = 128;
	}
	else if((dEx>0.01)&&(dEx<=256))
	{
		dMinPuleValue = 256;
	}
	else
	{
		dMinPuleValue = 512;
	}
	return dMinPuleValue;
}

//画Y方向标尺
void CMotionControlSoftwareView::DrawPuleLineX(CDC* pDC,CPoint begPt,CPoint endPt,double dMinPuleValue,CPoint endYPt)
{
	CPoint begPoint,endPoint,endYPoint;
	CPointEx begEx,endEx;
	begEx = LPointToMyPoint(begPt);
	endEx = LPointToMyPoint(endPt);

	CPoint tempPoint;

	int iBeg = begEx.m_dPX/dMinPuleValue;
	int iEnd = endEx.m_dPX/dMinPuleValue;
	if(iEnd<=iBeg)
		return;

	for(int i=iBeg;i<iEnd;i++)
	{
		begEx.m_dPX = dMinPuleValue*i;
		begPoint = MyPointToLPoint(begEx);
		endPoint = MyPointToLPoint(begEx);

		if(i%10 == 0)
		{
			tempPoint = CPoint(9,9);
			pDC->DPtoLP(&tempPoint);

			endPoint.y = tempPoint.y;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);

			endYPoint.x = begPoint.x;
			endYPoint.y = endYPt.y;
			DrawGridLine(pDC,begPoint,endYPoint);

			CString strValue = _T("");
			strValue.Format(_T("%f"),dMinPuleValue*i);
			pDC->SetBkMode(TRANSPARENT);
			pDC->TextOut(endPoint.x, endPoint.y+15, strValue,sizeof(strValue));
	//		pDC->TextOut(endPoint.x, 20, strValue,sizeof(strValue));
		}
		else if(i%5 == 0)
		{
			tempPoint = CPoint(13,13);
			pDC->DPtoLP(&tempPoint);

			endPoint.y = tempPoint.y;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);

			endYPoint.x = begPoint.x;
			endYPoint.y = endYPt.y;
			DrawGridLine(pDC,begPoint,endYPoint);
		}
		else
		{
			tempPoint = CPoint(17,17);
			pDC->DPtoLP(&tempPoint);

			endPoint.y = tempPoint.y;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);
		}
	}

}

//画Y方向标尺
void CMotionControlSoftwareView::DrawPuleLineY(CDC* pDC,CPoint begPt,CPoint endPt,double dMinPuleValue,CPoint endXPt)
{
	CPoint begPoint,endPoint,endXPoint;
	CPointEx begEx,endEx;
	begEx = LPointToMyPoint(begPt);
	endEx = LPointToMyPoint(endPt);

	CPoint tempPoint;

	int iBeg = begEx.m_dPY/dMinPuleValue;
	int iEnd = endEx.m_dPY/dMinPuleValue;
	if(iBeg<=iEnd)
		return;

	for(int i=iEnd;i<iBeg;i++)
	{
		begEx.m_dPY = dMinPuleValue*i;
		begPoint = MyPointToLPoint(begEx);
		endPoint = MyPointToLPoint(begEx);

		if(i%10 == 0)
		{
			tempPoint = CPoint(9,9);
			pDC->DPtoLP(&tempPoint);

			endPoint.x = tempPoint.x;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);

			endXPoint.x = endXPt.x;
			endXPoint.y = begPoint.y;
			DrawGridLine(pDC,begPoint,endXPoint);

			CString strValue = _T("");
			strValue.Format(_T("%f"),dMinPuleValue*i);
			pDC->SetBkMode(TRANSPARENT);
			pDC->TextOut(endPoint.x-15, endPoint.y, strValue,sizeof(strValue));
		}
		else if(i%5 == 0)
		{
			tempPoint = CPoint(13,13);
			pDC->DPtoLP(&tempPoint);

			endPoint.x = tempPoint.x;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);

			endXPoint.x = endXPt.x;
			endXPoint.y = begPoint.y;
			DrawGridLine(pDC,begPoint,endXPoint);
		}
		else
		{
			tempPoint = CPoint(17,17);
			pDC->DPtoLP(&tempPoint);

			endPoint.x = tempPoint.x;
			pDC->MoveTo(begPoint);
			pDC->LineTo(endPoint);
		}
	}
}

void CMotionControlSoftwareView::DrawGridLine(CDC* pDC,CPoint begPoint,CPoint endPoint)
{
	CPen *newPen,*oldPen;

	newPen = new CPen(PS_SOLID,1,RGB(215,215,215));
	oldPen=pDC->SelectObject(newPen);

	pDC->MoveTo(begPoint);
	pDC->LineTo(endPoint);
	
	pDC->SelectObject(oldPen);

	delete newPen;
}

//画鼠标当前位置线
void CMotionControlSoftwareView::DrawCurLine(CDC* pDC,CPoint curPt)
{
	CPoint begPoint,endPoint;
	CPoint tempPoint25,tempPoint5;

	tempPoint25 = CPoint(25,25);
	pDC->DPtoLP(&tempPoint25);
	tempPoint5 = CPoint(-5,-5);
	pDC->DPtoLP(&tempPoint5);

	//X当前线
	begPoint = endPoint = curPt;
	begPoint.y = tempPoint25.y;
	endPoint.y = tempPoint5.y;
	pDC->MoveTo(begPoint);
	pDC->LineTo(endPoint);

	//Y当前线
	begPoint = endPoint = curPt;
	begPoint.x = tempPoint25.x;
	endPoint.x = tempPoint5.x;
	pDC->MoveTo(begPoint);
	pDC->LineTo(endPoint);
}

//显示选中框
void CMotionControlSoftwareView::ShowSelectedRect(CDC* pDC)
{
	CMotionControlSoftwareDoc* pDoc = GetDocument();

	CRectEx curRect;
	if(pDoc->GetSelectedRect(curRect) == 0)
	{
		DrawSel9Rect(pDC,(curRect.m_left+curRect.m_right)/2.0,(curRect.m_top+curRect.m_bottom)/2.0);
		DrawSel9Rect(pDC,curRect.m_left,curRect.m_top);
		DrawSel9Rect(pDC,curRect.m_right,curRect.m_top);
		DrawSel9Rect(pDC,curRect.m_right,curRect.m_bottom);
		DrawSel9Rect(pDC,curRect.m_left,curRect.m_bottom);
	}
}

void CMotionControlSoftwareView::DrawSel9Rect(CDC* pDC,double dX,double dY)
{
	CMotionControlSoftwareDoc* pDoc = GetDocument();

	//转换到画图坐标
	int iCentX = dX*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.x;
	int iCentY = dY*m_iWinAndUserCoorRate+m_InitWinCenterLogicPos.y;

	CBrush brush(RGB(0,0,0));
	CRect rect(0,0,20,20);
	rect.left = iCentX-12;
	rect.top = iCentY-12;
	rect.right = iCentX+12;
	rect.bottom = iCentY+12;
	pDC->FillRect(&rect,&brush);
}


void CMotionControlSoftwareView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	Invalidate(TRUE);

	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}


void CMotionControlSoftwareView::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 1)
	{
		Invalidate(TRUE);

		KillTimer(1);
	}
	
	if (nIDEvent == 6789)
	{
		if (!m_bFirstRun)
		{
			CMotionControlSoftwareDoc* pDoc = GetDocument();
			if (pDoc->m_param.m_viewPara.m_iEnableShowLivePos == 1)
			{
				CString strPosX, strPosY, strPosZ;
				((CMainFrame*)(AfxGetMainWnd()))->m_wndMotionView.m_DlgMC.GetDlgItemText(IDC_STATIC_SHOWCURPOSITIONX,strPosX);
				((CMainFrame*)(AfxGetMainWnd()))->m_wndMotionView.m_DlgMC.GetDlgItemText(IDC_STATIC_SHOWCURPOSITIONY,strPosY);

				double dPosX = CTool::CStringToDouble(strPosX);
				double dPosY = CTool::CStringToDouble(strPosY);

				CPen pRed(PS_SOLID,5,RGB(255,0,0));
				CClientDC dc(this);
				OnPrepareDC(&dc);

				
				CPen* pOldPen = dc.SelectObject(&pRed);
				CPoint begPt, endPt;
				begPt.x = m_viewMath.make_int((dPosX-5)*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				begPt.y = m_viewMath.make_int(dPosY*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);
				endPt.x = m_viewMath.make_int((dPosX+5)*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				endPt.y = m_viewMath.make_int(dPosY*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);
				dc.MoveTo(begPt);
				dc.LineTo(endPt);

				begPt.x = m_viewMath.make_int(dPosX*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				begPt.y = m_viewMath.make_int((dPosY+5)*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);
				endPt.x = m_viewMath.make_int(dPosX*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				endPt.y = m_viewMath.make_int((dPosY-5)*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);
				dc.MoveTo(begPt);
				dc.LineTo(endPt);


				CPoint movePt, centerPt;
				int iRadius = m_viewMath.make_int(5*m_iWinAndUserCoorRate);
				centerPt.x = m_viewMath.make_int(dPosX*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				centerPt.y = m_viewMath.make_int(dPosY*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);

				//对于界面逻辑坐标系的Y轴方向向下,AngleArc画圆时是顺时针方向,画图的起始点是圆的终点,开始角度是:2*PI-结束角度=2*PI-(m_dStartAngle+m_dSweepAngle)
				movePt.x = m_viewMath.make_int((dPosX+5)*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.x);
				movePt.y = m_viewMath.make_int(dPosY*m_iWinAndUserCoorRate + m_InitWinCenterLogicPos.y);
				dc.MoveTo(movePt);
				dc.AngleArc(centerPt.x,centerPt.y,iRadius,0,360);	


				dc.SelectObject(pOldPen);
			}	
		}
	}

	CScrollView::OnTimer(nIDEvent);
}

//刷新更新界面
void CMotionControlSoftwareView::SetupTimer()
{
	// TODO: Add your message handler code here and/or call default
	//SetTimer(1,50,NULL);
}


void CMotionControlSoftwareView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default

	CScrollView::OnChar(nChar, nRepCnt, nFlags);
}


void CMotionControlSoftwareView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default

	CScrollView::OnKeyUp(nChar, nRepCnt, nFlags);
}


void CMotionControlSoftwareView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	BOOL bCtrl = GetKeyState(VK_CONTROL)< 0 ? TRUE : FALSE;
	if(bCtrl)//Ctrl键按下
	{
		if (nChar == 65) //全部选中
		{
			CMotionControlSoftwareDoc* pDoc = GetDocument();
			pDoc->SelectedAllList();
			Invalidate(TRUE);
			::PostMessage(((CMainFrame*)(AfxGetApp()->GetMainWnd()))->m_wndGraphView.m_graphSetPage.GetSafeHwnd(),SHOWGRAPHLIST,0,0);		
		}
	}
	CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}


BOOL CMotionControlSoftwareView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;
	return CScrollView::OnEraseBkgnd(pDC);
}
